//
//  BindingController.m
//  BCNADemo
//
//  Created by 马东朝 on 2018/5/17.
//  Copyright © 2018年 马东朝. All rights reserved.
//

#import "BindingController.h"
#define loginSty @"0"
@interface BindingController ()<LineTextFieldDelegate>
{
    UILabel *bindingLabel;
    UILabel *bindingStyleLabel;
    UILabel *verificationCodeLabel;
    LineTextField*verificationCodeTf;///验证码
    LineTextField *bindingTf;///账号
    UIButton *bindingBtn;
    NSMutableDictionary *bindingDic;
}
@end

@implementation BindingController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    bindingDic = [NSMutableDictionary dictionaryWithCapacity:0];
    [bindingDic setValue:[NSString enCryptKey:postKey strPassword:[[NSUserDefaults standardUserDefaults]valueForKey:UniqueU_id]] forKey:@"unique_id"];
    [bindingDic setValue:[NSString enCryptKey:postKey strPassword:[[NSUserDefaults standardUserDefaults] valueForKey:PassWord]] forKey:@"password"];
    [self addbindingStyleTitle];
    [self addbindingTfSubviews];
    [self addVerificationCodeSubViews];
    [self addBindingSubViews];
    if ([self.loginStyle isEqualToString:loginSty]) {
        [self setFameWithEmail];
    }else{
        [self setFrameWithIphoneNumber];
    }
}
- (void)addbindingStyleTitle{
    bindingLabel= [[UILabel alloc] initWithFrame:CGRectMake(kWidth(40), kHeight(235), kWidth(900), kHeight(70))];
    bindingLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    bindingLabel.font = [UIFont systemFontOfSize:kHeight(60)];
    if ([self.loginStyle isEqualToString:loginSty]) {
        bindingLabel.text = StringWithKey(@"BindingVC0");
    }else{
        bindingLabel.text = StringWithKey(@"BindingVC1");
    }
    
    [self.view addSubview:bindingLabel];
    
}
- (void)addbindingTfSubviews{
    bindingStyleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth(100), CGRectGetMaxY(bindingLabel.frame) + kHeight(90),ScreenWidth - kWidth(200), kHeight(60))];
    [self.view addSubview:bindingStyleLabel];
    if ([self.loginStyle isEqualToString:loginSty]) {
        bindingStyleLabel.text = StringWithKey(@"LgVC2");
    }else{
        bindingStyleLabel.text = StringWithKey(@"LgVC6");
    }
    
    bindingStyleLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    bindingStyleLabel.textAlignment = NSTextAlignmentLeft;
    bindingStyleLabel.font = [UIFont systemFontOfSize:kHeight(52)];
    bindingTf = [[LineTextField  alloc] initWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(bindingStyleLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(145))];
    
    if ([self.loginStyle isEqualToString:loginSty]) {
        [bindingTf setTextConentStyleWith:TextWayEmail];
    }else{
        [bindingTf setTextConentStyleWith:TextWayIphoneNumber];
    }
    
    [self.view addSubview:bindingTf];
}
///验证码
- (void)addVerificationCodeSubViews{
    verificationCodeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth(100), CGRectGetMaxY(bindingTf.frame) + kHeight(60),ScreenWidth - kWidth(200), kHeight(52))];
    
    [self.view addSubview:verificationCodeLabel];
    
    verificationCodeLabel.text = StringWithKey(@"RegistVC7");
    verificationCodeLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    verificationCodeLabel.textAlignment = NSTextAlignmentLeft;
    verificationCodeLabel.font = [UIFont systemFontOfSize:kHeight(52)];
    
    verificationCodeTf = [[LineTextField  alloc] initWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(verificationCodeLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(145))];
    verificationCodeTf.delegate = self;
    [verificationCodeTf setTextConentStyleWith:TextWayVerificationCode];
    [self.view addSubview:verificationCodeTf];
    
}
///重置按钮已经协议
- (void)addBindingSubViews{
    bindingBtn = [self setBtnWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(verificationCodeTf.frame) + kHeight(75), ScreenWidth-kWidth(180), kHeight(150)) title:StringWithKey(@"BindingVC2")];
    bindingBtn.backgroundColor = [UIColor colorWithHexString:@"#40b5f8"];
    [bindingBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:bindingBtn];
    [bindingBtn addTarget:self action:@selector(handleBinding) forControlEvents:UIControlEventTouchUpInside];
}
- (UIButton *)setBtnWithFrame:(CGRect) frame title:(NSString *)title{
    UIButton *abutton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    abutton.frame = frame;
    [abutton setTitle:title forState:UIControlStateNormal];
    abutton.layer.cornerRadius = frame.size.height/2;
    abutton.layer.masksToBounds = YES;
    
    return abutton;
}
- (void)setFameWithEmail{
    verificationCodeTf.hidden = YES;
    verificationCodeLabel.hidden = YES;
    
    bindingBtn.frame = CGRectMake(kWidth(90), CGRectGetMaxY(bindingTf.frame) + kHeight(75), ScreenWidth-kWidth(180), kHeight(150));
    //    goLoginBtn.frame = CGRectMake((ScreenWidth - kWidth(440))/2, CGRectGetMaxY(registBtn.frame) + kHeight(35), kWidth(440), kHeight(150));
}
- (void)setFrameWithIphoneNumber{
    verificationCodeTf.hidden = NO;
    verificationCodeLabel.hidden = NO;
    
    bindingBtn.frame = CGRectMake(kWidth(90), CGRectGetMaxY(verificationCodeTf.frame) + kHeight(75), ScreenWidth-kWidth(180), kHeight(150));
    //    goLoginBtn.frame = CGRectMake((ScreenWidth - kWidth(440))/2, CGRectGetMaxY(registBtn.frame) + kHeight(35), kWidth(440), kHeight(150));
    
    
}
#pragma mark --
- (void)handleBinding{
    
    
    NSMutableString *urlStr = [NSMutableString stringWithCapacity:0];
    [bindingTf resignFirstResponder];
    if (bindingTf.returnRight == YES ) {
        [bindingTf.textField resignFirstResponder];
    if ([self.loginStyle isEqualToString:loginSty]) {
        urlStr = [NSMutableString stringWithString:@"/user/bind-email"];
        [bindingDic setValue:[NSString enCryptKey:postKey strPassword:bindingTf.textField.text]  forKey:@"email"];
        [bindingDic setValue:[NSString enCryptKey:postKey strPassword:@"555555"] forKey:@"code"];
        [self bingDIngWithUrl:urlStr dic:bindingDic];
    }else{
        urlStr = [NSMutableString stringWithString:@"/user/bind-phone"];
         [bindingDic setValue:[NSString enCryptKey:postKey strPassword:bindingTf.textField.text] forKey:@"phone"];
         [bindingDic setValue:[NSString enCryptKey:postKey strPassword:[[NSUserDefaults standardUserDefaults] valueForKey:CodeStr]] forKey:@"code"];
        [verificationCodeTf resignFirstResponder];
        if (verificationCodeTf.returnRight == YES) {
       [verificationCodeTf.textField resignFirstResponder];
       
        [bindingDic setValue:[NSString enCryptKey:postKey strPassword:verificationCodeTf.textField.text]  forKey:@"sms_code"];
            [self bingDIngWithUrl:urlStr dic:bindingDic];
        }else{
            return;
        }
    }
    }else{
        return;
    }
    
}
- (void)bingDIngWithUrl:(NSString *)url dic:(NSDictionary *)dic{
    NSLog(@"%@===%@", url, dic);
    [GLHttpTool post:[NSString stringWithFormat:@"%@%@", UrlAddress, url] params:dic success:^(id responseObj) {
        NSLog(@"responseObj==%@", responseObj);
        NSString *code = [NSString stringWithFormat:@"%@", responseObj[@"code"]];
        NSString *msg = [NSString stringWithFormat:@"%@", responseObj[@"msg"]];
        if ([code isEqualToString:@"200"]) {
            if ([self.loginStyle isEqualToString:loginSty]) {
                [UserInfo sharedUserInfo].email = [NSMutableString stringWithString:bindingTf.textField.text];
                [[NSUserDefaults standardUserDefaults] setValue:bindingTf.textField.text forKey:EmailAdd];
            }else{
                [[NSUserDefaults standardUserDefaults] setValue:bindingTf.textField.text forKey:PhoneNum];
//                [UserInfo sharedUserInfo].phone = [NSMutableString stringWithString:bindingTf.textField.text];
            }
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            
            AlterView(StringWithKey(@"ALter0"), msg, StringWithKey(@"ALter8"));
            
        }
    } failure:^(NSError *error) {
        NSLog(@"error==%@", error);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)handleGetSMSCode{
    NSMutableDictionary *phoneDic= [NSMutableDictionary dictionaryWithCapacity:0];
    [bindingTf.textField resignFirstResponder];
    if (bindingTf.textField.text.length > 0) {
        [phoneDic setValue:[NSString enCryptKey:postKey strPassword:bindingTf.textField.text] forKey:@"phone"];
        [phoneDic setObject:[NSString enCryptKey:postKey strPassword:[[NSUserDefaults standardUserDefaults] valueForKey:CodeStr]] forKey:@"code"];
        [verificationCodeTf postSMSCodeWithPhoneDic:phoneDic];
    }else{
        AlterView(StringWithKey(@"ALter0"), StringWithKey(@"ALter6"), StringWithKey(@"ALter8"));
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
